package me.magicall.test.backup;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.HeadMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;

public class HttpUtils {

	public static final long GET_MAX_SIZE = 2097152L;

	public static String get(String url, String encode) {
		HttpClient client = null;
		GetMethod gm = null;
		String result = null;
		try {
			client = new HttpClient();
			client.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, encode);
			gm = new GetMethod(url);
			client.executeMethod(gm);
			result = gm.getResponseBodyAsString();
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		} finally {
			if (gm != null) {
				try {
					gm.releaseConnection();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
		return result;
	}

	public static String post(String url, String encode) {
		HttpClient client = null;
		PostMethod post = null;
		String result = null;
		try {
			client = new HttpClient();
			client.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, encode);
			post = new PostMethod(url);
			client.executeMethod(post);
			result = post.getResponseBodyAsString();
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		} finally {
			if (post != null) {
				try {
					post.releaseConnection();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
		return result;
	}

	public static int status(String url, String encode) {
		HttpClient client = new HttpClient();
		HeadMethod hm = new HeadMethod(url);
		client.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, encode);
		try {
			client.executeMethod(hm);
			return hm.getStatusCode();
		} catch (Exception e) {
			e.printStackTrace();
			return 500;
		} finally {
			hm.releaseConnection();
		}
	}

	public static final String getCookie(HttpServletRequest request, String cookieName) {
		Cookie[] cookies = request.getCookies();
		if (cookies != null) {
			for (Cookie cookie : cookies) {
				if (cookie.getName().equals(cookieName)) {
					return cookie.getValue();
				}
			}
		}
		return null;
	}

	public static final void addCookie(HttpServletResponse response, String cookieName, String cookieValue) {
		Cookie cookie = new Cookie(cookieName, cookieValue);
		response.addCookie(cookie);
	}

	public static final void clearCookie(HttpServletResponse response, String cookieName) {
		Cookie cookie = new Cookie(cookieName, null);
		cookie.setMaxAge(0);
		response.addCookie(cookie);
	}
    
	public static void main(String[] arg) {
//		HttpClient client = null;
//		GetMethod gm = null;
//		String result = null;
//		try {
//			client = new HttpClient();
//			client.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "utf-8");
//			gm = new GetMethod("http://passport.no.opi-corp.com/verify.php?t=u78c97vvagnphpnl37okssmqj3");
//			gm.setRequestHeader("Referer", "/index");
//			client.executeMethod(gm);
//			result = gm.getResponseBodyAsString();
//			System.out.println("result  " + result);
//		} catch (Exception e) {
//			e.printStackTrace();
//		} finally {
//			if (gm != null) {
//				try {
//					gm.releaseConnection();
//				} catch (Exception e) {
//					e.printStackTrace();
//				}
//			}
//		}
		
//		System.out.println(post("https://passport.no.opi-corp.com/verify.php?t=u78c97vvagnphpnl37okssmqj3", "utf-8"));
	}
}
